package com.ymatou.iapi.trading.sellerquery2.testcase;

import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.sellerquery2.parameter.GetOrderSummaryInfoBean;
import com.ymatou.iapi.trading.sellerquery2.parameter.resp.GetOrderSummaryInfoResp;
import com.ymatou.query.seller.trading.api.service.OrderCreate;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymt.utils.tag.P3;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.constant.ECode;

/**
 * 获取订单物流信息 FreeMaker Template自动生成代码
 * 接口已经转接到java
 */
@TestSuite
@Deprecated
//@Ignore
public class Ts_GetOrderSummaryInfo {

	private static GetOrderSummaryInfoBean gosibean;
	private static com.ymatou.iapi.trading.sellerquery2.service.GetOrderSummaryInfoCall gosicall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取订单物流信息");
	}

	@Before
	public void caseUp() {

		gosibean = new GetOrderSummaryInfoBean();
		gosicall = new com.ymatou.iapi.trading.sellerquery2.service.GetOrderSummaryInfoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(gosicall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verify() {
		GetOrderSummaryInfoResp expectResponse = new GetOrderSummaryInfoResp();
		ConstructExpectedResult(expectResponse);
		GetOrderSummaryInfoResp actualResponse = gosicall
				.getOrderSummaryInfoResp();
		DataUtil.verify(expectResponse.getOrderSummaryInfo(),
				actualResponse.getOrderSummaryInfo(), "验证OrderSummaryInfo");
	}

	@SuppressWarnings("rawtypes")
	private void ConstructExpectedResult(GetOrderSummaryInfoResp response) {
		List<Map> results = execute(gosibean.getOrderId());
		String orderSummaryInfo = DataUtil.isNullOrEmpty(results)
				? null
				: DataUtil.Stringg.getValueOrNull(results.get(0)
						.get("sSummary"));
		response.setOrderSummaryInfo(orderSummaryInfo);
	}

	@SuppressWarnings("rawtypes")
	public static List<Map> execute(int orderId) {
		return new DefaultWapper().executeSql(GetOrderSummaryInfo(orderId));
	}

	public static String GetOrderSummaryInfo(int orderId) {
		return "SELECT TOP 1 sSummary FROM [Ymt_OrderSummary] (nolock)"
				+ "where iOrderId = " + orderId + " order by dAddTime desc";
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderSummaryInfo_001() {
		Logger.start(true, "创建已发货状态的订单，获取订单物流信息成功");
		try {
			Logger.comment("准备数据");
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			OrderCreate o = new OrderCreate();
			int orderId = o.create3_ShippedOrder(userId, sellerId)
					.getiOrderId();
			Thread.sleep(500l);
			Logger.comment("准备数据完成");

			// 请求参数
			gosibean.setOrderId(orderId);
			// 发送请求
			String s = FormatUtil.beanToHttpString(gosibean, true, false);
			gosicall.setData(s);
			gosicall.callService();
			//			Logger.comment("gosicall send:" + gosicall.getEntityString());
			//			Logger.comment("gosicall response:" + gosicall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, gosicall.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_GetOrderSummaryInfo_002() {
		Logger.start(true, "创建下单状态的订单，获取订单物流信息=null");
		try {
			Logger.comment("准备数据");
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			OrderCreate o = new OrderCreate();
			int orderId = o.create1_M2cOrder(userId, sellerId).getiOrderId();
			Thread.sleep(500l);
			Logger.comment("准备数据完成");

			// 请求参数
			gosibean.setOrderId(orderId);
			// 发送请求
			String s = FormatUtil.beanToHttpString(gosibean, true, false);
			gosicall.setData(s);
			gosicall.callService();
			//			Logger.comment("gosicall send:" + gosicall.getEntityString());
			//			Logger.comment("gosicall response:" + gosicall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, gosicall.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_GetOrderSummaryInfo_003() {
		Logger.start(true, "创建付款状态的订单，获取订单物流信息=null");
		try {
			Logger.comment("准备数据");
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			OrderCreate o = new OrderCreate();
			int orderId = o.create2_PayOrder(userId, sellerId).getiOrderId();
			Thread.sleep(500l);
			Logger.comment("准备数据完成");

			// 请求参数
			gosibean.setOrderId(orderId);
			// 发送请求
			String s = FormatUtil.beanToHttpString(gosibean, true, false);
			gosicall.setData(s);
			gosicall.callService();
			//			Logger.comment("gosicall send:" + gosicall.getEntityString());
			//			Logger.comment("gosicall response:" + gosicall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, gosicall.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetOrderSummaryInfo_004() {
		Logger.start(false, "订单号为空，获取订单物流信息失败");
		try {
			// 发送请求
			String s = FormatUtil.beanToHttpString(gosibean, true, false);
			gosicall.setData(s);
			gosicall.callService();
			//			Logger.comment("gosicall send:" + gosicall.getEntityString());
			//			Logger.comment("gosicall response:" + gosicall.getReturnData());
			Logger.verifyEquals(ECode.FAILED400, gosicall.getCode(), "验证Code");
			Logger.verifyEquals(ECode.BCODE8, gosicall.getBCode(), "验证BCode");
			Logger.verifyEquals("orderId:订单号不能为0", gosicall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderSummaryInfo_005() {
		Logger.start(true, "订单号不存在，获取订单物流信息为空，成功");
		try {
			// 发送请求
			gosibean.setOrderId(1111111111);
			String s = FormatUtil.beanToHttpString(gosibean, true, false);
			gosicall.setData(s);
			gosicall.callService();
			//			Logger.comment("gosicall send:" + gosicall.getEntityString());
			//			Logger.comment("gosicall response:" + gosicall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, gosicall.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

}